package org.ResearchProject.controller;

import org.ResearchProject.dto.Result;
import org.ResearchProject.dto.ThisProjectPeopleDto;
import org.ResearchProject.pojo.ResearchProject;
import org.ResearchProject.service.ResearchProjectService;
import org.ResearchProject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * 科研项目管理
 */

@RestController
@RequestMapping("/project")
public class ResearchProjectController {
    @Autowired
    ResearchProjectService researchProjectService;
    /**
     * 添加科研项目
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result addResearchProject(@RequestBody @Validated ResearchProject researchProject) {
        Map<String, Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        // 插入科研项目
        researchProject.createPerson = (int) msg.get("id");
        researchProjectService.insertResearchProject(researchProject);
        System.out.println("添加科研项目");
        return Result.success();
    }

    /**
     * 删除科研项目
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result deleteResearchProject(Integer projectId) {
        Map<String, Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        // 删除科研项目
        researchProjectService.deleteProject(projectId);
        return Result.success();
    }

    /**
     * 修改科研项目
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result updateResearchProject(@RequestBody ResearchProject researchProject) {
        researchProjectService.updateProject(researchProject);
        return Result.success("项目修改成功!");
    }

    /**
     * 添加参与科研项目成员
     */
    @RequestMapping(value = "/addMembers",method = RequestMethod.POST)
    public Result addMembers(@RequestParam ArrayList<Integer> members,Integer projectId) {
        System.out.println(members);
        Map<String, Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        // 添加成员
        researchProjectService.addMembers(members,projectId,(int) msg.get("id"));
        return Result.success("项目成员添加成功!");
    }

    /**
     * 查询科研项目参与成员
     */
    @RequestMapping(value = "/ThisProjectPeople",method = RequestMethod.GET)
    public Result ThisProjectPeople(int projectId) {
        ThisProjectPeopleDto dto = researchProjectService.getThisProjectPeople(projectId);
        System.out.println(dto);
        return Result.success(dto);
    }

    /**
     * 删除科研项目组成员
     */
    @RequestMapping(value = "/deleteMembers",method = RequestMethod.POST)
    public Result deleteMembers(@RequestParam ArrayList<Integer> members,Integer projectId) {
        System.out.println(members);
        Map<String, Object> msg = ThreadLocalUtil.get();
        if ((int)msg.get("auth")!=0)
            return Result.error("没有权限！");
        // 删除成员
        researchProjectService.deleteMembers(members,projectId,(Integer) msg.get("id"));
        return Result.success("项目成员删除成功!");
    }
}
